package br.com.devteam.sgap.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import br.com.devteam.base.converter.Menuable;

@NamedQueries({
	@NamedQuery(name = Tema.QUERY_LIST_ASC_ORDER,
			query = "select t from Tema t order by nome"),
	@NamedQuery(name = Tema.QUERY_SEARCH,
			query = "select t from Tema t where lower(t.nome) like lower(:busca)"),
	@NamedQuery(name = Tema.QUERY_NOME,
			query = "select t from Tema t where t.nome = :nome"),
})
@Entity
@Table
public class Tema implements Menuable {
	
	public static final String QUERY_LIST_ASC_ORDER = "Tema.listAscOrder";
	public static final String QUERY_SEARCH = "Tema.search";
	public static final String QUERY_NOME = "Tema.nome";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column
	private Integer id;
	
	@Column(nullable = false)
	private String nome;
	
	@OneToMany(mappedBy = "tema", fetch = FetchType.EAGER, orphanRemoval = true)
	@Cascade(value = {CascadeType.ALL})
	private List<Questao> questoes;
	
	public Tema() {
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public List<Questao> getQuestoes() {
		return questoes;
	}

	public void setQuestoes(List<Questao> questoes) {
		this.questoes = questoes;
	}

	@Override
	public Object getIdentifier() {
		return getId() != null ? getId() : -1;
	}

	@Override
	public int hashCode() {
		return getId() != null ? getId() : -1;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		
		if (!(obj instanceof Tema))
			return false;
		
		if (getId() != null && getId().equals(((Tema) obj).getId()))
			return true;
		
		return super.equals(obj);
	}

}
